#include "multiplebuttonform.h"
#include "ui_multiplebuttonform.h"
#include <QPainter>
#include <QStyleOption>

MultipleButtonForm::MultipleButtonForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MultipleButtonForm)
{
    ui->setupUi(this);
    this->setFixedHeight(30);
    ui->widget_hide->hide();
}

MultipleButtonForm::~MultipleButtonForm()
{
    delete ui;
}

void MultipleButtonForm::ShowAll()
{
    ui->widget_hide->show();
    this->setFixedHeight(100);
}

void MultipleButtonForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void MultipleButtonForm::on_pushButton_level1_clicked(bool checked)
{
    if(!checked)
    {
        if(ui->pushButton_level2->isChecked())
        {
            ui->pushButton_level1->setChecked(true);
            ui->pushButton_level2->setChecked(false);
            ui->pushButton_level3->setChecked(false);
            emit levelChanged(1); // level1选中
        }
        else
        {
            emit levelChanged(0); // 都没选中
        }
    }
    else
    {
        if(!ui->pushButton_level2->isChecked() && !ui->pushButton_level3->isChecked())
        {
            emit levelChanged(1); // 只有level1选中
        }
        else if(ui->pushButton_level2->isChecked() && !ui->pushButton_level3->isChecked())
        {
            emit levelChanged(2); // level1和level2选中
        }
        else if(ui->pushButton_level2->isChecked() && ui->pushButton_level3->isChecked())
        {
            emit levelChanged(3); // 全部选中
        }
    }
}

void MultipleButtonForm::on_pushButton_level2_clicked(bool checked)
{
    if(!checked)
    {
        if(ui->pushButton_level3->isChecked())
        {
            ui->pushButton_level3->setChecked(false);
            ui->pushButton_level2->setChecked(true);
            emit levelChanged(2); // level1和level2选中
        }
        else if(ui->pushButton_level1->isChecked())
        {
            emit levelChanged(1); // 只有level1选中
        }
        else
        {
            emit levelChanged(0); // 都没选中
        }
    }
    else {
        ui->pushButton_level1->setChecked(true);
        if(ui->pushButton_level3->isChecked())
        {
            emit levelChanged(3); // 全部选中
        }
        else
        {
            emit levelChanged(2); // level1和level2选中
        }
    }
}

void MultipleButtonForm::on_pushButton_level3_clicked(bool checked)
{
    if(checked)
    {
        ui->pushButton_level1->setChecked(true);
        ui->pushButton_level2->setChecked(true);
        emit levelChanged(3); // 全部选中
    }
    else
    {
        if(ui->pushButton_level2->isChecked())
        {
            emit levelChanged(2); // level1和level2选中
        }
        else if(ui->pushButton_level1->isChecked())
        {
            emit levelChanged(1); // 只有level1选中
        }
        else
        {
            emit levelChanged(0); // 都没选中
        }
    }
}
